<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>

        // const queryStr = window.location.href.split('?')[1];
        
        // console.log(window.decodeURIComponent(queryStr)); // 解码
        // console.log(window.encodeURIComponent(queryStr)); // 编码

        const BASE_URL = 'http://www.liulongbin.top:3006';

        // params = { id: 1, name: 'zhangsan' }
        function getParamsStr (params) {
            let str = '';
            for (const key in params) {
                const value = params[key];
                str += `${key}=${value}&`;
            }
            return str.slice(0, -1); // ?id=1&name=zhangsan
        }

        // // 封装jq的$.get
        // function $get (url, params, cb) {
        //     const xhr = new XMLHttpRequest();
        //     xhr.onreadystatechange = function () {
        //         console.log(xhr.readyState);
        //         if (xhr.readyState === 4) {
        //             const res = {
        //                 status: xhr.status,
        //                 data: JSON.parse(xhr.responseText),
        //             };
        //             cb(res);
        //         }
        //     };
        //     xhr.open('GET', url + getParamsStr(params)); // ?id=1&name=zhangsan
        //     xhr.send();
        // }

        // $get(BASE_URL + '/api/getbooks', { author: '罗贯中' }, (res) => {
        //     console.log(res);
        // });

        function $post (url, reqBody, cb) {
            const xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4) {
                    const res = {
                        status: xhr.status,
                        data: JSON.parse(xhr.responseText),
                    };
                    cb(res);
                }
            };
            xhr.open('POST', url);
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xhr.send(getParamsStr(reqBody));
        }

        const body = {
            author: 'yuzelin',
            bookname: 'sadawe',
            publisher: 'dasdas',
        };
        $post(BASE_URL + '/api/addbook', body, (res) => {
            console.log(res);
        });


        // $.ajax

        // axios.get
        
        // axios(config)


    </script>
</body>
</html>